import os
import time

def if_max_task(max_task_num, sleeptime):
    user_name = os.popen("echo $USER").readlines()[0].strip('\n')
    sub_task = os.popen("squeue | grep " + user_name + " | wc -l").readlines()[0].strip('\n')
    while int(sub_task) >= max_task_num:
        print("\nmax task number is {0}, sleep {1}s\n".format(str(max_task_num),sleeptime))
        os.system("squeue")
        time.sleep(sleeptime)
        sub_task = os.popen("squeue | grep " + user_name + " | wc -l").readlines()[0].strip('\n')

def sbatchfile(dir,subinfo):
    max_task_num = subinfo["max_task_num"]
    sleeptime = subinfo["sleep_time"]
    if_max_task(max_task_num, sleeptime)
    filepath = os.path.join(dir,"mysub.slurm")
    res = os.system("sbatch {0}".format(filepath))
    if res:
        print("sub {0}\n: info: {1}".format(filepath,res))